<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;
use App\Http\Request\Api\v1\Article\InfoRequest;
use App\Http\Request\Api\v1\Article\ListRequest;
use App\Models\Arts\ArticleModel;
use App\Models\Arts\ArticleTypeModel;
use App\Utils\AppResult;

/**
 * 文章示例
 * Author:panda
 * Email:joeyoung0314@qq.com
 * Class ArticleController
 * @package App\Http\Controllers\Api\v1
 */
class ArticleController extends Controller
{
    // 日志模块名称
    const LOG_MODULE = 'v1/ArticleController';

    /**
     * 文章分类
     * @return string
     */
    public function type()
    {
        $model = new ArticleTypeModel();

        $list = $model->getList(
            orderBy: [
                'sort'       => 'desc',
                'created_at' => 'desc',
            ]
        );

        return AppResult::response200(result: $list);
    }

    /**
     * 文章列表
     * @param ListRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(ListRequest $request)
    {
        $params = $request->validated();

        $model = new ArticleModel();

        $list = $model->getList(
            params : $params,
            orderBy: [
                'sort'       => 'desc',
                'created_at' => 'desc',
            ],
            select: ['id', 'type_id', 'title', 'image', 'description', 'created_at']
        );

        return AppResult::response200(result: $list);
    }

    /**
     * 详情
     * @param InfoRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(InfoRequest $request)
    {
        $params = $request->validated();

        $model = new ArticleModel();

        $info = $model->getDetail($params);

        if (!$info) {
            return AppResult::response201('信息不存在了.');
        }

        return AppResult::response200(result: $info);
    }
}
